package com.u2b.business.sort;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

//按照距离和时间排序
public class SortProxy implements Comparator<HashMap<String, Object>>
{
    private String key;
    private SimpleDateFormat simpleDateFormat = null;

    public SortProxy(String key)
    {
        if ("create_time".equals(key))
        {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
    }

    @Override
    public int compare(HashMap<String, Object> o1, HashMap<String, Object> o2)
    {
        if ("distance".equals(key))
        {
            return Double.valueOf(o1.get(key).toString()) > Double.valueOf(o2.get(key).toString()) ? 0 : 1;
        }
        else if ("create_time".equals(key))
        {
            try
            {
                long time1 = simpleDateFormat.parse(o1.get("create_time").toString()).getTime();
                long time2 = simpleDateFormat.parse(o2.get("create_time").toString()).getTime();
                return time1 > time2 ? 0 : 1;
            }
            catch (ParseException e)
            {

                throw new RuntimeException("日期转换出现错误", e);
            }

        }
        return 1;
    }

    public static void sortList(List<HashMap<String, Object>> values, String key)
    {

        Collections.sort(values, new SortProxy(key));
    }

}
